//
//  BDGroupListViewController.m
//  BadBoy
//
//  Created by wen on 2017/7/24.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDGroupListViewController.h"
#import "BDFridentTableViewCell.h"
#import <JMessage/JMessage.h>

@interface BDGroupListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *datasource;

@end

@implementation BDGroupListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = ASLocalizedString(@"我的群聊");
    _datasource = [NSMutableArray array];
    [self requestDatasource];
}

- (void)requestDatasource{
    [JMSGGroup myGroupArray:^(id resultObject, NSError *error) {
        if (error) {
            
        }else{
            [self.datasource addObjectsFromArray:resultObject];
            [self.tableview reloadData];
            [self setTableiewFooterViewStateFor:self.tableview datasourceCount:self.datasource.count];
        }
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 65;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datasource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"cellID";
    BDFridentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[BDFridentTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [JMSGGroup groupInfoWithGroupId:self.datasource[indexPath.row] completionHandler:^(id resultObject, NSError *error) {
        JMSGGroup *group = resultObject;
        [cell.avatarImage sd_setImageWithURL:[NSURL URLWithString:group.avatar] placeholderImage:[UIImage imageNamed:@"qun_touxiang"]];
        cell.nameLabel.text = group.name;
    }];
    
    cell.applyCountLabel.hidden = YES;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [JMSGGroup groupInfoWithGroupId:self.datasource[indexPath.row] completionHandler:^(id resultObject, NSError *error) {
        JMSGGroup *group = resultObject;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"jumpToNewGroupChat" object:group];
    }];
}

- (UITableView *)tableview{
    if (!_tableview) {
        self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, kBDTopHeight, kBDScreenWidth, kBDScreenHeight - kBDTopHeight) style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
        _tableview.sectionIndexTrackingBackgroundColor = [UIColor clearColor];
        _tableview.sectionIndexBackgroundColor = [UIColor clearColor];
        [self.view addSubview:_tableview];
    }
    return _tableview;
}

- (NSMutableArray *)datasource{
    if (!_datasource) {
        self.datasource = [NSMutableArray array];
    }
    return _datasource;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
